//
//  ToolViewController.m
//  TravelEveryDay
//
//  Created by 路洪宇 on 15/10/24.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "ToolViewController.h"
#import "MyLocationModel.h"
#import "ToolCityViewController.h"
#import "BaseHeader.h"
@interface ToolViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView* _tableView;
    NSInteger _currentSection;
    NSInteger _currentRow;
    NSInteger _selectCellNum;
}
@property(nonatomic, retain) NSMutableArray *headViewArray;
@property(nonatomic, retain) UITableView* tableView;
@property(nonatomic,retain)NSMutableArray *keyArray;

@property(nonatomic,retain)NSMutableArray *cityArray;
@property(nonatomic,retain)NSMutableArray *bigCityArray;
@property(nonatomic,retain)NSMutableDictionary *bigDic;
@property(nonatomic,retain)NSMutableArray *selectArray;


@property(nonatomic,retain)UITableView *rightTableView;


@end

@implementation ToolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    _keyArray =[[NSMutableArray alloc] init];
    _bigCityArray = [[NSMutableArray alloc] init];
    _selectArray = [[NSMutableArray alloc] init];
    
    [self setUpTitleView];
    [self SetUpTabelView];
    [self setUpDartView];
    [self SetUpRightTabelView];
    [self getLocationData];
    
}


-(void)setUpTitleView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [titleView setBackgroundColor:TITLECOLOR];
    [self.view addSubview:titleView];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [backButton setFrame:CGRectMake(10, 30, 22, 22)];
    [backButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 25, 200, 30)];
    [title setText:@"工具箱"];
    [title setTextColor:[UIColor whiteColor]];
    title.font = [UIFont systemFontOfSize:19.];
    title.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:title];
    
    
    [self.view addSubview:titleView];
    
}

-(void)SetUpTabelView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64,SCREEN_WIDTH,SCREEN_HEIGHT-120) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self.view addSubview:_tableView];
}

-(void)SetUpRightTabelView
{
    _rightTableView = [[UITableView alloc]init];
    _rightTableView.frame = CGRectMake(SCREEN_WIDTH, 64,250, SCREEN_HEIGHT-64);
    _rightTableView.delegate = self;
    _rightTableView.tableFooterView = [[UIView alloc] init];
    _rightTableView.dataSource = self;
    _rightTableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick :)];
    
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    
    [_rightTableView addGestureRecognizer:swipe];
    [self.view addSubview:_rightTableView];
}

- (void)getLocationData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations.json?page=1"];
    [AFNDataBlock string:urlStr AFNData:^(id block) {
        for (NSMutableDictionary *bigDic in block) {
            
            for (NSMutableDictionary *dic in [bigDic objectForKey:@"destinations"])
            {
                MyLocationModel *model = [MyLocationModel modelWithDictionary:dic];
                model.mId = [dic objectForKey:@"id"];
                [_keyArray addObject:model];
                
                _cityArray  = [[NSMutableArray alloc]init];
                for (NSMutableDictionary *cityDic in [dic objectForKey:@"children"]) {
                    MyLocationModel *cityModel = [MyLocationModel modelWithDictionary:cityDic];
                    cityModel.mId = [cityDic objectForKey:@"id"];
                    [_cityArray addObject:cityModel];
                }
                [_bigCityArray addObject:_cityArray];
            }
        }
        [self.tableView reloadData];
        [_rightTableView reloadData];
    }];
}

-(void)leftClick
{
#pragma mark- 返回指定页面
    UIViewController *vc =[self.navigationController.viewControllers objectAtIndex:0];
    [self.navigationController popToViewController:vc animated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
}



#pragma mark - TableViewdelegate&&TableViewdataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        return _keyArray.count;
    }
    else
    {
        return _selectArray.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        
        static NSString *cellId = @"UITableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        MyLocationModel *tempModel = [_keyArray objectAtIndex:indexPath.row];
        cell.backgroundColor =[UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.text = tempModel.name_zh_cn;
        return cell;
        
    }
    if (tableView == _rightTableView) {
        static NSString *cellId = @"RigthUITableViewCell";
        UITableViewCell *rightCell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (rightCell == nil)
        {
            rightCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        }
        rightCell.backgroundColor =[UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        rightCell.selectionStyle = UITableViewCellSelectionStyleNone;
        MyLocationModel *tempModel = [_selectArray objectAtIndex:indexPath.row];
        rightCell.textLabel.textColor = [UIColor grayColor];
        rightCell.textLabel.text = tempModel.name_zh_cn;
        return rightCell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([tableView isEqual:_tableView]) {
        self.darkView.hidden = NO;
        [_selectArray removeAllObjects];
        NSLog(@"点击左cell:%ld",indexPath.row);
        _selectArray = [NSMutableArray arrayWithArray:[_bigCityArray objectAtIndex:indexPath.row]];
        [_rightTableView reloadData];
        [UIView animateWithDuration:0.2 animations:^{
            [self.rightTableView setFrame:CGRectMake(SCREEN_WIDTH-250, 64,250, SCREEN_HEIGHT-64)];
        }];
    }
    if (tableView == _rightTableView) {
        MyLocationModel *tempModel = [_selectArray objectAtIndex:indexPath.row];
        ToolCityViewController *toolCityVC = [[ToolCityViewController alloc] init];
        toolCityVC.tempModel = tempModel;
        [self.navigationController pushViewController:toolCityVC animated:YES];
    }
}

#pragma mark- 遮罩图
-(void)setUpDartView
{
    self.darkView = [[UIButton alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.darkView setBackgroundColor:[UIColor blackColor]];
    [self.darkView addTarget:self action:@selector(darkClick) forControlEvents:UIControlEventTouchUpInside];
    self.darkView.alpha = 0.5;
    self.darkView.hidden = YES;
    [self.view addSubview:self.darkView];
}

#pragma mark- ButtonClick
-(void)darkClick
{
    [UIView animateWithDuration:0.2 animations:^{
        [self.rightTableView setFrame:CGRectMake(SCREEN_WIDTH, 64,130, SCREEN_HEIGHT-64)];
        self.darkView.hidden = YES;
    }];
}

#pragma mark-滑动手势
-(void)swipeClick:(UISwipeGestureRecognizer *)swipe
{
    [UIView animateWithDuration:0.2 animations:^{
        [self.rightTableView setFrame:CGRectMake(SCREEN_WIDTH, 64,130, SCREEN_HEIGHT-64)];
        self.darkView.hidden = YES;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
